package crawler.web.utils;

import java.io.File;
import java.io.Serializable;

import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.StoreConfig;
import com.sleepycat.persist.model.Entity;
import com.sleepycat.persist.model.PrimaryKey;

public class URLDPL {
	@Entity
	static class VisitedURL implements Serializable {
		private static final long serialVersionUID = 1153459749365294603L;
		@PrimaryKey
		private String url;

		public String getUrl() {
			return url;
		}

		public void setUrl(String url) {
			this.url = url;
		}
	}

	static class SimpleDA {
		// Index Accessors
		PrimaryIndex<String, VisitedURL> pIdx;

		// Open the indices
		public SimpleDA(EntityStore store) throws DatabaseException {
			// Primary key for SimpleEntityClass classes
			pIdx = store.getPrimaryIndex(String.class, VisitedURL.class);
		}
	}

	public URLDPL(String dbPath) {
		envHome = new File(dbPath);
		setup();
	}

	//Next we create a method that simply opens our database environment and entity store for us.
	// The setup() method opens the environment and store
	// for us.
	public void setup() throws DatabaseException {
		EnvironmentConfig envConfig = new EnvironmentConfig();
		StoreConfig storeConfig = new StoreConfig();

		envConfig.setAllowCreate(true);
		storeConfig.setAllowCreate(true);

		if (!envHome.exists())
			envHome.mkdirs();
		// Open the environment and entity store
		envmnt = new Environment(envHome, envConfig);
		store = new EntityStore(envmnt, "EntityStore", storeConfig);
		sda = new SimpleDA(store);
		Runtime.getRuntime().addShutdownHook(new Thread() {
			@Override
			public void run() {
				shutdown();
			}
		});
	}

	//We also need a method to close our environment and store.
	// Close our environment and store.
	public void shutdown() throws DatabaseException {
		store.close();
		envmnt.close();
	}

	private File envHome;
	private Environment envmnt;
	private EntityStore store;
	private SimpleDA sda;

	public boolean contains(String url) {
		return sda.pIdx.contains(url);
	}

	//Now we need to create a method to actually write objects to our store. This method creates a SimpleDA object (see SimpleDA.class that we will use to access our indexes. Then we instantiate a series of CrawlURL (see A Simple Entity Class) objects that we will place in our store. Finally, we use our primary index (obtained from the SimpleDA class instance) to actually place these objects in our store.
	//In Retrieving Objects from an Entity Store we show a class that is used to retrieve these objects.
	// Populate the entity store
	public void save(String url) throws DatabaseException {
		// Instantiate and store some entity classes
		VisitedURL visitedURL = new VisitedURL();
		visitedURL.setUrl(url);
		sda.pIdx.put(visitedURL);
	}

	public void clear() {
		store.truncateClass(VisitedURL.class);
	}

	public long count() {
		return sda.pIdx.count();
	}
}
//
//
//
//
//
//
//
//
//
//